package com.lanvideo.dao;

import com.lanvideo.entity.VideoViewRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 视频观看记录数据访问层
 */
@Repository
public class VideoViewRecordDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 记录视频观看
     */
    public int recordView(Integer videoId, Integer userId, Integer watchDuration) {
        String sql = "INSERT INTO video_view_record (video_id, user_id, watch_duration) VALUES (?, ?, ?) " +
                "ON DUPLICATE KEY UPDATE watch_duration = watch_duration + ?, view_time = CURRENT_TIMESTAMP";
        return jdbcTemplate.update(sql, videoId, userId, watchDuration, watchDuration);
    }

    /**
     * 增加视频观看次数
     */
    public int incrementViewCount(Integer videoId) {
        String sql = "UPDATE video SET view_count = view_count + 1 WHERE id = ?";
        return jdbcTemplate.update(sql, videoId);
    }

    /**
     * 获取用户的观看记录
     */
    public List<VideoViewRecord> getUserViewRecords(Integer userId) {
        String sql = "SELECT vvr.*, v.title as video_title, u.name as user_name " +
                "FROM video_view_record vvr " +
                "LEFT JOIN video v ON vvr.video_id = v.id " +
                "LEFT JOIN user u ON vvr.user_id = u.id " +
                "WHERE vvr.user_id = ? " +
                "ORDER BY vvr.view_time DESC";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(VideoViewRecord.class), userId);
    }

    /**
     * 获取视频的观看记录
     */
    public List<VideoViewRecord> getVideoViewRecords(Integer videoId) {
        String sql = "SELECT vvr.*, v.title as video_title, u.name as user_name " +
                "FROM video_view_record vvr " +
                "LEFT JOIN video v ON vvr.video_id = v.id " +
                "LEFT JOIN user u ON vvr.user_id = u.id " +
                "WHERE vvr.video_id = ? " +
                "ORDER BY vvr.view_time DESC";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(VideoViewRecord.class), videoId);
    }

    /**
     * 检查用户是否观看过视频
     */
    public boolean hasUserViewed(Integer userId, Integer videoId) {
        String sql = "SELECT COUNT(*) FROM video_view_record WHERE user_id = ? AND video_id = ?";
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class, userId, videoId);
        return count != null && count > 0;
    }

    /**
     * 获取用户观看视频的总时长
     */
    public Integer getUserTotalWatchDuration(Integer userId, Integer videoId) {
        String sql = "SELECT watch_duration FROM video_view_record WHERE user_id = ? AND video_id = ?";
        Integer duration = jdbcTemplate.queryForObject(sql, Integer.class, userId, videoId);
        return duration != null ? duration : 0;
    }

    /**
     * 删除观看记录
     */
    public int deleteViewRecord(Integer id) {
        String sql = "DELETE FROM video_view_record WHERE id = ?";
        return jdbcTemplate.update(sql, id);
    }

    /**
     * 获取热门视频（按观看次数排序）
     */
    public List<VideoViewRecord> getPopularVideos(int limit) {
        String sql = "SELECT v.id as video_id, v.title as video_title, v.view_count, " +
                "COUNT(vvr.id) as view_count, " +
                "MAX(vvr.view_time) as view_time " +
                "FROM video v " +
                "LEFT JOIN video_view_record vvr ON v.id = vvr.video_id " +
                "WHERE v.status = 1 " +
                "GROUP BY v.id, v.title, v.view_count " +
                "ORDER BY v.view_count DESC, COUNT(vvr.id) DESC " +
                "LIMIT ?";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(VideoViewRecord.class), limit);
    }
}
